<template>
  <div class>
    <el-dialog :title="title" :visible.sync="dialogFormVisible" width="600px" @close="close">
      <el-form ref="form" :model="form" label-width="120px">
        <el-form-item label="排序" prop="sort">
          <el-input v-model.trim="form.sort" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="标签名称" prop="name">
          <el-input v-model.trim="form.name" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="medium" @click="close">取 消</el-button>
        <el-button size="medium" type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { storeCategory } from '@/api/shop';
export default {
  name: 'goodlistedit',
  created() {},
  data() {
    return {
      form: {
        id: '',
        name: '',
        sort: '',
        item: '3'
      },
      data: {},
      title: '',
      dialogFormVisible: false
    };
  },
  components: {},
  methods: {
    showEdit(row) {
      if (!row) {
        this.title = '添加';
      } else {
        this.title = '编辑';
        this.form = Object.assign({}, row);
      }
      this.dialogFormVisible = true;
    },
    close() {
      this.$refs['form'].resetFields();
      this.form = this.$options.data().form;
      this.dialogFormVisible = false;
    },
    save() {
      this.$refs['form'].validate(async (valid) => {
        if (valid) {
          this.form2 = Object.assign({}, this.data);
          this.form2.id = this.form.id;
          this.form2.item = this.form.item;
          this.form2.sort = this.form.sort;
          this.form2.name = this.form.name;
          const { msg } = await storeCategory(this.form2);
          this.$baseMessage(msg, 'success');
          this.$emit('fetchData');
          this.close();
        } else {
          return false;
        }
      });
    }
  }
};
</script>
